//
//  XLUserAccountTools.swift
//  微博
//
//  Created by macbook on 16/9/29.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit

class XLUserAccountTools: NSObject {
    
    let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    
    //保存的数据对象
    var userAccount:XLUserAccount?
    
    //判断用户是否登陆
    var isLogin : Bool{
    
        //如果token为空,代表没有登录
        return accessToken != nil
    }
    
    //访问令牌
    var accessToken:String?{
        //代表没有登录
        if userAccount?.access_token == nil {
            
            return nil
        }else
        {
            //和目前时间比降序的话 代表没有过时
            if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                
                return userAccount?.access_token
            }else
            {
                return nil
            }
        }
    
    }
    
    //全局访问点
    static let shardTools:XLUserAccountTools = XLUserAccountTools()
    
    override init() {
        super.init()
        userAccount = getUserAccount()
    }
    
    //保存对象
    func saveUserAccount(userAccount:XLUserAccount){
    
        //单例对象只有在第一调用的时候可以赋值,所以立马赋值
        self.userAccount = userAccount
        
        //路径
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: path)
    }
    
    //获取对象
    func getUserAccount() ->XLUserAccount?{
        let restult = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? XLUserAccount
        
        return restult
    }
    
    
    
    
    
    
    

}
